#include <iostream>
#include <vector>

template <class T>
class less
{
public:
    bool operator()(const T &x, const T &y) const
    {
        return x < y;
    }
};

template <class T>
class greater
{
public:
    bool operator()(const T &x, const T &y) const
    {
        return x > y;
    }
};

template <class T, class Container = std::vector<T>, class Compare = less<T>>
class priority_queue
{
public:
    // 无参构造
    priority_queue() {}

    // 迭代器构造
    template <class InputIterator>
    priority_queue(InputIterator first, InputIterator last)
        : _con(first, last)
    {
        // 向下调整建堆
        for (int i = (_con.size() - 1 - 1) / 2; i >= 0; i--)
        {
            adjust_down(i);
        }
    }

    void push(const T &val)
    {
        _con.push_back(val);
        adjust_up(_con.size() - 1);
    }

    void pop()
    {
        std::swap(_con[0], _con[_con.size() - 1]);
        _con.pop_back();
        adjust_down(0);
    }

    const T &top() const
    {
        return _con[0];
    }

    bool empty() const
    {
        return _con.empty();
    }

    size_t size() const
    {
        return _con.size();
    }

private:
    void adjust_down(int parent)
    {
        Compare com;
        int n = _con.size();
        int maxchild = 2 * parent + 1;
        while (maxchild < n)
        {
            if (maxchild + 1 < n && _con[maxchild] < _con[maxchild + 1])
            {
                maxchild++;
            }
            if (com(_con[parent], _con[maxchild]))
            {
                std::swap(_con[parent], _con[maxchild]);
                parent = maxchild;
                maxchild = 2 * parent + 1;
            }
            else
                break;
        }
    }

    void adjust_up(int child)
    {
        Compare com;
        int parent = (child - 1) / 2;
        while (child > 0)
        {
            if (com(_con[parent], _con[child]))
            {
                std::swap(_con[parent], _con[child]);
                child = parent;
                parent = (child - 1) / 2;
            }
            else
                break;
        }
    }

private:
    Container _con;
};